package leilei.jace.com.tcpdemo3.test;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.lianyi.push.entity.TcpMsgBean;
import com.lianyi.push.share.ConstantValue;
import com.lianyi.push.share.listener.TcpSeiviceToClientMessageListener;
import com.lianyi.push.share.listener.UdpSeiviceToClientMessageListener;
import com.lianyi.push.share.tcpclient.TcpClient;
import com.lianyi.push.share.udpclient.UdpClient;
import com.lianyi.push.util.CommonUtils;
import com.lianyi.push.util.LogUtil;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.socket.DatagramPacket;
import io.netty.util.CharsetUtil;
import leilei.jace.com.tcpdemo3.R;

public class MainActivity extends AppCompatActivity {
    private ScreenReceiver screenReceiver = new ScreenReceiver();
    private EditText edttext;
    private TextView showip, showcontent;
    private ExecutorService executorService = Executors.newFixedThreadPool(10);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        IntentFilter filter = new IntentFilter();
        filter.addAction("action.SCREEN_ON");
        registerReceiver(screenReceiver, filter);
        showip = (TextView) findViewById(R.id.showip);
        showcontent = (TextView) findViewById(R.id.showcontent);
        edttext = (EditText) findViewById(R.id.edttext);
        showip.setText("本机IP:" + CommonUtils.getLocalIpAddress());
    }

    public void starClient(View view) {
        TcpClient.getInstance().StartTcpClient(edttext.getText().toString(), new TcpSeiviceToClientMessageListener() {
            @Override
            public void tcpSeiviceToClientMessage(ChannelHandlerContext channelHandlerContext, Object o) {

            }
        });
    }

    public void cleanText(View view) {
        showcontent.setText("");
        showip.setText("本机IP:" + CommonUtils.getLocalIpAddress());
    }

    public void sendMsg(View view) {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("[{\"0\":\"0\"}");
        for (int i = 0; i < 1000; i++) {
            stringBuffer.append(",");
            stringBuffer.append("{\"" + (i + 1) + ":\"" + (i + 1) + "\"}");
        }
        stringBuffer.append("]");
        TcpMsgBean tcpMsgBean = new TcpMsgBean(ConstantValue.TCP_HEART_DATE, stringBuffer.toString(), stringBuffer.length());
        TcpClient.getInstance().SendMessage(tcpMsgBean);
    }

    public void startService(View view) {
        Intent intent = new Intent(MainActivity.this, MyService.class);
        startService(intent);
    }

    public void isConnect(View view) {
        boolean isConnect = TcpClient.getInstance().isConnect(edttext.getText().toString());
        LogUtil.e("isConnect", isConnect + "==");
    }

    public void startUdpClient(View view) {
        UdpClient.getInstance().StartUdpClient("收到UDP广播:" + CommonUtils.getLocalIpAddress(), new UdpSeiviceToClientMessageListener() {
            @Override
            public void serviceToClientListener(ChannelHandlerContext ctx, DatagramPacket datagramPacket) {
                final String response = datagramPacket.content().toString(CharsetUtil.UTF_8);
                Log.e("客户端收到服务器消息response", "客户端收到服务器消息response:" + response);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        showcontent.setText("客户端收到服务器消息response:" + response);
                    }
                });
            }
        });
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(screenReceiver);
    }

    public class ScreenReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, final Intent intent) {
            Log.e("消息广播", "消息广播");
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(MainActivity.this, intent.getStringExtra("msg"), Toast.LENGTH_SHORT).show();
//                    Toast.makeText(MainActivity.this, "警告，手机电量过低,将会马上爆炸，请远离手机!", Toast.LENGTH_SHORT).show();
                    showcontent.setText(intent.getExtras().getString("msg") + "");
                }
            });
        }

    }
}
